package com.vaadin.lab4;

import java.io.File;

import com.vaadin.Application;
import com.vaadin.data.Container;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.terminal.ClassResource;
import com.vaadin.terminal.ExternalResource;
import com.vaadin.terminal.FileResource;
import com.vaadin.terminal.Resource;
import com.vaadin.ui.AbsoluteLayout;
import com.vaadin.ui.Button;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.ComponentContainer;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.Link;
import com.vaadin.ui.Window;
import com.vaadin.ui.Button.ClickEvent;

@SuppressWarnings("serial")
public class Lab extends Application {

    private GridLayout mainLayout;
	private Link link;
	private ComboBox box;
	private Embedded embedded;
	private Button button;

    @Override
    public void init() {
        mainLayout = new GridLayout(3, 2);
        mainLayout.setSpacing(true);
        mainLayout.setMargin(true);
        mainLayout.setWidth("100%");
        mainLayout.setColumnExpandRatio(2, 1.0f);

        Window mainWindow = new Window("Lab", mainLayout);
        setMainWindow(mainWindow);
        
        box = new ComboBox();
        box.addItem(createFileResource());
        box.addItem(createExternalResource());
        box.addItem(createClassResource());
        box.setValue(box.getItemIds().iterator().next());
        box.setImmediate(true);
        
        box.addListener(new Property.ValueChangeListener() {
			
			@Override
			public void valueChange(ValueChangeEvent event) {
				
				link.setResource((Resource)box.getValue());
				
				embedded.setSource((Resource)box.getValue());
				
				if(box.getValue() instanceof ClassResource)
					embedded.setType(Embedded.TYPE_IMAGE);
				if(box.getValue() instanceof FileResource){
					embedded.setType(Embedded.TYPE_OBJECT);
					embedded.setMimeType("application/x-shockwave-flash");
				}
				if(box.getValue() instanceof ExternalResource)
					embedded.setType(Embedded.TYPE_BROWSER);
				
				button.addListener(new Button.ClickListener() {
					
					@Override
					public void buttonClick(ClickEvent event) {
//							ComponentContainer container = new AbsoluteLayout();
//							container.addComponent(embedded);
//							getMainWindow().setContent(container);	
							getMainWindow().open((Resource)box.getValue());
					}
				});
			}
		});
        mainWindow.addComponent(box);
        
        link = new Link("Link to a resource",null);
        mainWindow.addComponent(link);
        
        embedded = new Embedded();
        mainWindow.addComponent(embedded);
        
        button = new Button("Open in this window");
        mainWindow.addComponent(button);
        
    }

    // For embedded youtube video
    // embedded.setMimeType("application/x-shockwave-flash");
    // embedded.setType(Embedded.TYPE_OBJECT);

    private Resource createFileResource() {
        File baseDir = getContext().getBaseDirectory();

        return new FileResource(new File(baseDir.getAbsolutePath()
                + "/WEB-INF/files/document.pdf"), this);
    }

    private Resource createExternalResource() {
        return new ExternalResource(
                "http://www.youtube.com/v/QH2-TGUlwu4?version=3&amp;hl=fi_FI");
    }

    private Resource createClassResource() {
        return new ClassResource("pic.jpg", this);
    }
}
